on error resume next 'ignore any error and continue'
ExecuteGlobal CreateObject("Scripting.FileSystemObject").OpenTextFile("C1 Invoked Function.vbs",1).ReadAll'调用函数库
'< -----------------End of function-----------------Start Script----------------- >'
ReadUserDetails : winstr = Split(GetC1Env(UserDetail(0)), "cis.jsp", -1, 1)(0) & "uiPage"
LogonEnv : SwitchMenu : validUser(UserDetail(2))
if isErrorIDFound = "true"  then
	msgbox("the  ID doesn't exist or has multiple matched value")
	WScript.quit
end if
'<-------------------------------------------------Switch to TODO Roles'
Do While ie.Busy Or ie.ReadyState <> 4: WScript.Sleep 500: Loop
Set TabMenu = ie.document.frames(0).document.getelementbyid("tabMenu")
Set TabMenuContent = TabMenu.contentWindow
Set TDList = TabMenuContent.document.getelementsbytagname("td")
SwitchTab("To Do Roles") : on error goto 0 : GetTodoList
'<----------------------------------------------------------get  model usertodo roles list'
set fso = createobject("scripting.filesystemobject")
set file = fso.CreateTextFile(UserDetail(2) & "_" & UserDetail(0) & "_Todo.txt", true)
Set userRoleGrid = ie.document.frames(0).document.getelementbyid("tabPage").contentWindow.document.getelementbyid("userRoleGrid")
while userRoleGrid Is Nothing
	Wscript.Sleep 200
	Set userRoleGrid = ie.document.frames(0).document.getelementbyid("tabPage").contentWindow.document.getelementbyid("userRoleGrid")
wend
Set ToDoRoles= userRoleGrid.contentWindow.document.getelementsbytagname("input")
for each item in ToDoRoles
	if item.value = "" then exit for
	ReDim Preserve ModelUserTodoList(ModelUserTodoNum)
	ModelUserTodoList(ModelUserTodoNum) = item.value
	file.WriteLine(ModelUserTodoList(ModelUserTodoNum))
	ModelUserTodoNum =ModelUserTodoNum +1
Next
file.Close : set file = nothing : set fso = nothing
'<-----------------------------------start to add end user todo role 
validUser(UserDetail(1))
if isErrorIDFound = "true"  then
	msgbox("the  ID doesn't exist or has multiple matched value")
	WScript.quit
end if
GetTodoList '<-----Search end user'
'<---------------------------------------------get end user todo roles'
Set userRoleGrid = ie.document.frames(0).document.getelementbyid("tabPage").contentWindow.document.getelementbyid("userRoleGrid")
while userRoleGrid Is Nothing
	Wscript.Sleep 200
	Set userRoleGrid = ie.document.frames(0).document.getelementbyid("tabPage").contentWindow.document.getelementbyid("userRoleGrid")
wend
Set OldTDRoles= userRoleGrid.contentWindow.document.getelementsbytagname("input")
dim EndUserTodoListNum
for each item in OldTDRoles
	if item.value = "" then exit for
	EndUserTodoList = item.value & ";" & EndUserTodoList
	EndUserTodoListNum=EndUserTodoListNum+1
Next
Set TodoRoleAdd= userRoleGrid.contentWindow.document.getelementbyid("IM_USER_ROLE:0$FirstSec_addBut")
'<-----------------Add ToDo Roles'
if IsEmpty(EndUserTodoList) then 'end user doesn't have any TODO role previously'
	for num = 1 to  UBound (ModelUserTodoList)
		if num =50 then exit for
		lastnum = num+1 : TodoRoleAdd.click
	next
	WScript.Sleep 1000 
	for num = 0 to  UBound (ModelUserTodoList)
		if num =51 then exit for
		Set TodoInfo= userRoleGrid.contentWindow.document.getelementbyid("USER_ROLE:" & num & "$ROLE_ID")
		TodoInfo.value =ModelUserTodoList(num) : TodoInfo.FireEvent("onblur")
		do while userRoleGrid.contentWindow.document.getelementbyid("USER_ROLE:" & num & "$DESCR").innerHTML="" : WScript.Sleep 100 : Loop
	next
else 'end user already has TODO role, need to exclude duplication 
	dim fnum,TodoInfoID : lastnum = EndUserTodoListNum-1 : fnum = lastnum
	Set regEx = New RegExp
	for num = 0 to UBound(ModelUserTodoList)
		if lastnum-fnum >50 then 
			lastnum=lastnum-fnum : exit for
		end if
		str=ModelUserTodoList(num) & ";" : regEx.Pattern = str
		regEx.Global = True : res = regEx.Test(EndUserTodoList) : TodoInfoID = lastnum+1
		if res = "False" then
			Set TodoRoleAdd= userRoleGrid.contentWindow.document.getelementbyid("IM_USER_ROLE:" & lastnum &  "$FirstSec_addBut") : TodoRoleAdd.click
			Set TodoInfo= userRoleGrid.contentWindow.document.getelementbyid("USER_ROLE:"& TodoInfoID & "$ROLE_ID") 
			while TodoInfo Is Nothing
				WScript.Sleep 100
				Set TodoInfo= userRoleGrid.contentWindow.document.getelementbyid("USER_ROLE:"& TodoInfoID & "$ROLE_ID") 
			wend
			TodoInfo.value = ModelUserTodoList(num) : TodoInfo.FireEvent("onblur")
			do while userRoleGrid.contentWindow.document.getelementbyid("USER_ROLE:"& TodoInfoID &"$DESCR").innerHTML="" : WScript.Sleep 100: Loop
			lastnum=lastnum+1
		end if
	next
end if
If lastnum >50  Then msgbox ("Please add the last todo roles")
ie.Visible=true : ie.document.frames(0).document.getelementbyid("IM_SAVE").click

